package cn.dengta.webapp.im.entity;

import java.util.Date;
import java.util.List;

import cn.dengta.webapp.base.entity.I18nContent;
import cn.dengta.webapp.base.entity.I18nData;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.X;

/**
 * 默契问答的问题表
 *
 * @author lizhichao
 * @since 2022-06-22
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("im_question")
public class Question extends TimeBasedEntity {

	private static final long serialVersionUID = 1L;

	/** 关键词支持的语言标识，多个以英文逗号隔开 */
	private String languages;
	/** 关键词的多语言JSON，对应的值为关键字词 */
	private String keyword;
	/** 问题的多语言JSON，对应的值为问题内容 */
	private String question;
	/** 回答的多语言JSON，对应的值为字符串数组 */
	private String answers;

	protected transient I18nData<List<String>> keywordResolvedLanguageCfg;
	protected transient I18nContent questionResolvedLanguageCfg;
	protected transient I18nData<List<String>> answersResolvedLanguageCfg;

	public static final String LANGUAGES = "languages";
	public static final String KEYWORD = "keyword";
	public static final String QUESTION = "question";
	public static final String ANSWERS = "answers";

	public void init(Date now) {
		if (!hasValidId()) {
			addTime = now;
		}
		updateTime = now;
	}

	public I18nData<List<String>> getKeywordLanguageCfg() {
		I18nData<List<String>> cfg = keywordResolvedLanguageCfg;
		if (cfg == null) {
			keywordResolvedLanguageCfg = cfg = X.castType(I18nData.deserialize(keyword, List.class));
		}
		return cfg;
	}

	public I18nContent getQuestionResolvedLanguageCfg() {
		I18nContent cfg = questionResolvedLanguageCfg;
		if (cfg == null) {
			questionResolvedLanguageCfg = cfg = I18nContent.deserialize(question);
		}
		return cfg;
	}

	public I18nData<List<String>> getAnswersResolvedLanguageCfg() {
		I18nData<List<String>> cfg = answersResolvedLanguageCfg;
		if (cfg == null) {
			answersResolvedLanguageCfg = cfg = X.castType(I18nData.deserialize(answers, List.class));
		}
		return cfg;
	}

}
